package com.apps.ftapp.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.MySubjectCollListAdapter;
import com.apps.ftapp.model.MyCollListModel;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;

/**
 * 我的收藏列表
 */
public class MySubjectCollListActivity extends BaseActivity {

    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private List<MyCollListModel.DataBean> dataList = new ArrayList();
    private MySubjectCollListAdapter mySubjectCollListAdapter;


    private Gson gson = new Gson();

    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_subject_coll_list);
        ButterKnife.bind(this);


        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        String id = bundle.getString("id");
        String name = bundle.getString("name");
        type = bundle.getString("type");

        setTitle(name);


        L.i("main", id);


        initView();


        loadData(id);


    }

    private void loadData(String targetId) {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        params.put("targetType", type);
        params.put("targetId", targetId);

        L.i("main", "token:" + Constant.token + ":::" + Constant.uid + "::::" + targetId + "::::" + type);

        Async.post("my/favorite/question/list", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", ">>>>>" + new String(responseBody));
                    MyCollListModel result = new MyCollListModel();
                    try {
                        result = gson.fromJson(new String(responseBody), MyCollListModel.class);
                    } catch (Exception e) {

                    }
                    if (1 == Tools.isIntNull(result.getSuccess())) {
                        dataList.addAll(result.getData());
                        mySubjectCollListAdapter.addRes(result.getData());
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                L.i("main", ">>>>>" + new String(responseBody));
            }
        });

    }

    private void initView() {


        mySubjectCollListAdapter = new MySubjectCollListAdapter(getApplicationContext(), dataList);
        listView.setAdapter(mySubjectCollListAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                HashMap<String, Object> data = new HashMap<String, Object>();
                data.put("id", dataList.get(position).getQuestionId());
//                L.i("main","::::"+dataList.get(position).getType());
//                if("tiku".equals(dataList.get(position).getType())){
                ActivitySkipUtil.skipAnotherActivity(MySubjectCollListActivity.this, MyErrorDetailActivity.class, data);
//                }else{
//                    ActivitySkipUtil.skipAnotherActivity(MySubjectCollListActivity.this, CuotiJieXiActivity.class,data);
//                }


//                data.put("name",dataList.get(i).getName());
//                data.put("type",dataList.get(i).getType());

            }
        });


    }
}
